<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="" class="w-1/5">
          <a-input v-model:value="formState.zjm" placeholder="药品编号/药品名称/助记码/姓名/助记码/手机号" allow-clear />
        </a-form-item>
        <a-form-item name="dateRange" label="发药日期">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="dispensingSn" label="" class="w-[260px] ml-2">
          <a-input v-model:value="formState.dispensingSn" placeholder="发药流水号" allow-clear />
        </a-form-item>
        <a-form-item label="" class="w-56 ml-6">
          <a-radio-group v-model:value="formState.sfpj">
            <a-radio value="">全部</a-radio>
            <a-radio value="0">针剂</a-radio>
            <a-radio value="1">片剂</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-3">
        <a-button type="primary" class="mr-2" ghost @click="onPrint"><BaseIcon name="PrinterOutlined" />发药单</a-button>
        <BaseExport
          type="住院"
          name="西成药发药查询"
          code="000354"
          :search-params="{
            stdate: formState.dateRange[0],
            eddate: formState.dateRange[1],
            zjm: formState.zjm,
            dispensingSn: formState.dispensingSn,
            sfpj: formState.sfpj,
            operationmode: 2
          }"
          >导出</BaseExport
        >
      </div>
      <BaseTable :loading="loading" column-code="000354" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg, cj: record?.cj }" />
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 10)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  zjm: '',
  sfpj: ''
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/zyyf/getStatisticsDistributeDrugList',
  params: formState.value,
  formatParams: (params) => {
    return {
      stdate: params['dateRange']?.[0],
      eddate: params['dateRange']?.[1],
      zjm: formState.value.zjm,
      dispensingSn: formState.value.dispensingSn,
      sfpj: formState.value.sfpj,
      operationmode: 2
    }
  }
})

const onPrint = async () => {
  if (tableData.value.length > 0) {
    const uniqueData = tableData.value.filter((item, index, self) => index === self.findIndex((t) => t.dispensingSn === item.dispensingSn))
    uniqueData.forEach(async (item) => {
      const { code, msg, data } = await post('/inp/Print/inpDispensingMedicinesPrint', {
        fyshr: item.updateUser,
        dispensingSn: item.dispensingSn,
        wardName: item.wardName,
        yjlx: item.yjlx
      })
      if (code !== 0) return message.error(msg)
      window.openExe?.rpt(data) //调用winform打印
    })
  }
}

onMounted(() => {
  getData()
})
</script>
